<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-06-17
 * Time: 15:15:56
 * Info:
 */

namespace app\common\model;

use app\common\service\CacheService;

class AuthMenu extends Common
{

    public static function manageAdd($param)
    {
        //查询规则地址是否存在
        $findRule = AuthMenu::where('node', $param['node'])->find();
        if ( ! empty($findRule)) {
            return ['code' => 0, 'msg' => '该菜单已经存在'];
        }
        /*if ($param['pid'] == 0) {
            $param['type'] = 0;
        } else {
            $param['type'] = 1;
        }*/
        AuthMenu::create($param);
        CacheService::updateMenu();

        return ['code' => 200, 'msg' => '添加成功'];
    }

    public static function manageEdit($param)
    {
        //查询当前规则是否存在
        $findRule = AuthMenu::where('id', $param['id'])->find();
        if (empty($findRule)) {
            return ['code' => 0, 'msg' => '该菜单不存在'];
        }
        //查询规则地址是否存在
        $findRuleHref = AuthMenu::where('node', $param['node'])->where('id', '<>', $param['id'])->find();
        if ( ! empty($findRuleHref)) {
            return ['code' => 0, 'msg' => '该菜单已存在'];
        }
        $where = ['id' => $param['id']];
        /*if ($param['pid'] == 0) {
            $param['type'] = 0;
        } else {
            $param['type'] = 1;
        }*/
        AuthMenu::update($param, $where);
        CacheService::updateMenu();

        return ['code' => 200, 'msg' => '保存成功'];
    }

    public static function manageDel($id)
    {
        $findAuthRule = AuthMenu::where('id', $id)->find();
        if (empty($findAuthRule)) {
            return ['code' => 0, 'msg' => '规则不存在'];
        }
        //查询是否有子级
        $findSon = AuthMenu::where(['pid' => $findAuthRule['id']])->select();
        if ( ! $findSon->isEmpty()) {
            return ['code' => 0, 'msg' => '请先删除子级规则'];
        }
        AuthMenu::destroy($findAuthRule['id']);
        CacheService::updateMenu();

        return ['code' => 200, 'msg' => '删除成功'];
    }

    public static function getPidMenuList()
    {
        $list        = AuthMenu::field('id,pid,title')->where('status', 1)->select()->toArray();
        $pidMenuList = AuthMenu::buildPidMenu(0, $list);
        $pidMenuList = array_merge([
            [
                'id'    => 0,
                'pid'   => 0,
                'title' => '顶级菜单',
            ]
        ], $pidMenuList);

        return $pidMenuList;
    }

    /**
     * 根据id查询数据
     * @param $id
     */
    public static function findData($id)
    {
        $findData = AuthMenu::where('id', $id)->find();
        if (empty($findData)) {
            return ['code' => 0, 'msg' => '获取数据失败'];
        }

        return ['code' => 200, 'msg' => 'ok', 'data' => $findData];
    }

    public static function manageModify($param)
    {
        $findData = AuthMenu::findData($param['id']);
        if ($findData['code'] == 0) {
            return $findData;
        }
        $param[$param['field']] = $param['val'];
        AuthMenu::where(['id' => $param['id']])->strict(false)->data($param)->update();
        CacheService::updateMenu();

        return ['code' => 200, 'msg' => '更改成功'];
    }

    protected static function buildPidMenu($pid, $list, $level = 0)
    {
        $newList = [];
        foreach ($list as $vo) {
            if ($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                if ($level > 1) {
                    $repeatString = "----";
                    $markString   = str_repeat("│{$repeatString}", $level - 1);
                    $vo['title']  = $markString.$vo['title'];
                }
                $newList[] = $vo;
                $childList = AuthMenu::buildPidMenu($vo['id'], $list, $level);
                ! empty($childList) && $newList = array_merge($newList, $childList);
            }

        }

        return $newList;
    }
}
